package com.cafe.action.admin;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.cafe.model.Customer;
import com.cafe.model.Gender;
import com.cafe.service.CustomerService;
import com.malamute.core.action.BaseAction;
import com.malamute.core.action.CrudMethod;
import com.malamute.core.common.SearchParamUtil;

/**
 * @Title:CustomerAction.java
 * @Package com.cafe.action
 * @Description 后台会员管理Action
 * @author AceGuan gjybst@163.com
 * @date 2012-1-30 下午02:53:23
 * @version v1.0
 */
/**
 * 跳转分为两类： 1.链接地址跳转 2.Action跳转 type = "redirect"为直接跳转页面 type = "redirectAction"
 * 跳转Action
 */
// @Results({
// @Result(name = BaseAction.LIST, location = "${listResult}.jsp"),// 查询时跳转到列表页面
// @Result(name = BaseAction.SUCCESS, location = "/ccccc.jsp"),// 查询时跳转到列表页面
// @Result(name = BaseAction.FORM, location = "customer_form.jsp"),// 编辑页面
// @Result(name = BaseAction.ACTION, type = "redirectAction", params = {
// "actionName", "customer!list" }) // 保存成功后列表Action 跳转
// })
// @Action(value = "customer!*", params = { "method", "{1}" })
public class CustomerAction extends BaseAction implements CrudMethod {
	@Autowired(required = false)
	private CustomerService customerService;
	private Map<String, String> genderMap = new HashMap<String, String>();
	private Customer customer;

	/**
	 * 获取customer
	 * 
	 * @return customer
	 */

	public Customer getCustomer() {
		return customer;
	}

	/**
	 * 设置customer
	 * 
	 * @param customer
	 */
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	/**
	 * 获取genders，性别map返回提供选择
	 * 
	 * @return genders
	 */

	public Map<String, String> getGenderMap() {
		for (Gender g : Gender.values()) {
			genderMap.put(g.getKey(), g.getValue());
		}
		return genderMap;
	}

	public String input() throws Exception {
		setCustomer(getCustomer());
		return INPUT;
	}

	public String form() throws Exception {
		if (customer != null) {
			customer = customerService.getCustomerById(customer.getId());
		}
		return FORM;
	}

	public String list() throws Exception {
		// System.out.println(this.getNameSpace());
		DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
		// for (Iterator it = searchParam.keySet().iterator(); it.hasNext();) {
		// String key = (String) it.next();
		// String[] values = (String[]) searchParam.get(key);
		// criteria.add(Restrictions.like(key, "%" + values[0] + "%"));
		// }
		SearchParamUtil.simpleSeach(criteria, searchParam);
		// TODO :这里又可以做修改了。。跳转页面按照默认规则
		// setListResult("customer_main");
		// page.setCurrentPageNum(1);
		customerService.listCustomersByPage(criteria, page);
		System.out.println(page.getTotalPageNum());
		return LIST;
	}

	public String search() throws Exception {
		return this.list();
	}

	public String save() throws Exception {
		customerService.saveCustomer(customer);
		return ACTION;
	}

	public String update() throws Exception {
		customerService.updateCustomer(customer);
		return ACTION;
	}

	public String openViewCustomer() {
		if (customer.getId() != 0) {
			customer = customerService.getCustomerById(customer.getId());
		}
		return "open";
	}

	public void prepareList() {
	}

}
